<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>大事件-后台首页</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css" />
    <link rel="stylesheet" href="css/reset.css" />
    <link rel="stylesheet" href="css/iconfont.css" />
    <link rel="stylesheet" href="css/main.css" />
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <!-- 导入bootstrap的js文件 -->
    <script src="./libs/bootstrap/js/bootstrap.min.js"></script>
  </head>

  <body>
    <div class="sider">
      <a href="#" class="logo"><img src="images/logo02.png" alt="logo"/></a>
      <div class="user_info">
        <img src="images/2.jpg" alt="person" />
        <span>欢迎&nbsp;&nbsp;李思思</span>
        <b>管理员</b>
      </div>
      <!-- 左侧导航栏 -->
      <div class="menu">
        <div class="level01 active">
          <a href="./main_count.html" target="main_body"
            ><i class="iconfont icon-yidiandiantubiao04"></i
            ><span>首页</span></a
          >
        </div>

        <div class="level01">
          <a href="./article_list.html" target="main_body"
            ><i class="iconfont icon-icon-article"></i><span>文章管理</span
            ><b class="iconfont icon-arrowdownl"></b
          ></a>
        </div>

        <ul class="level02">
          <li>
            <a href="./article_list.html" target="main_body"
              ><i class="iconfont icon-previewright"></i
              ><span>文章列表</span></a
            >
          </li>
          <li>
            <a href="./article_release.html" target="main_body"
              ><i class="iconfont icon-previewright"></i
              ><span>发表文章</span></a
            >
          </li>
          <li>
            <a href="./article_category.html" target="main_body"
              ><i class="iconfont icon-previewright"></i
              ><span>文章类别管理</span></a
            >
          </li>
        </ul>

        <div class="level01">
          <a href="./comment_list.html" target="main_body"
            ><i class="iconfont icon-comment"></i><span>评论管理</span></a
          >
        </div>
        <div class="level01" id="user">
          <a href="./user.html" target="main_body"
            ><i class="iconfont icon-user"></i><span>个人中心</span></a
          >
        </div>
      </div>
      <!-- 顶部栏 -->
      <div class="header_bar">
        <div class="user_center_link">
          <a href="./user.html" target="main_body" id="user2">个人中心</a>
          <img src="images/2.jpg" alt="person" />
          <a href="javascript:void(0)" class="logout"
            ><i class="iconfont icon-tuichu"></i> 退出</a
          >
        </div>
      </div>
      <!-- 右侧主体内容 -->
      <div class="main" id="main_body">
        <!-- 9.1 存放页面的容器 -->
        <iframe
          src="./main_count.html"
          name="main_body"
          style="width: 100%; height: 100%;"
        ></iframe>
      </div>
    </div>

    <!-- 3.1 引入模态框结构 -->
    <div class="modal fade" tabindex="-1" role="dialog" id="myModal">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button
              type="button"
              class="close"
              data-dismiss="modal"
              aria-label="Close"
            >
              <span aria-hidden="true">&times;</span>
            </button>
            <h4 class="modal-title">温馨提示</h4>
          </div>
          <div class="modal-body">
            <p>One fine body&hellip;</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">
              确认
            </button>
          </div>
        </div>
        <!-- /.modal-content -->
      </div>
      <!-- /.modal-dialog -->
    </div>
    <!-- /.modal -->
  </body>
</html>

<!-- 1. 引入地址js文件 -->
<script src="./libs/https.js"></script>

<script>
  $(function () {
    // 2. 发送ajax请求，渲染用户信息
    $.ajax({
      type: 'get',
      url: BigNew.user_info,
      success: function (backData) {
        // console.log(backData)
        // 3. 问题：被禁止访问，获取本地存储的token  代码放在jq文件中

        // 4. 渲染页面
        let data = backData.data
        if (backData.code == 200) {
          $('.user_info img').attr('src', data.userPic)
          $('.user_center_link img').attr('src', data.userPic)
          $('.user_info span').html(`欢迎&nbsp;&nbsp;${data.nickname}`)
        }
      },

      // 6. 错误监听
      error: function (xhr, status, err) {
        if (err == 'Forbidden') {
          // 7. 未经过登入直接输网址进入
          $('.modal-body p').text('请先登入')
          $('#myModal').modal()
          $('#myModal').on('hidden.bs.modal', function () {
            localStorage.removeItem('token')
            window.location = './login.html'
          })
        }
      }
    })

    // 5. 退出登入，删除本地token 跳转回登入页面
    $('.icon-tuichu, .logout').on('click', function () {
      $('.modal-body p').text('退出成功')
      $('#myModal').modal()
      $('#myModal').on('hidden.bs.modal', function () {
        localStorage.removeItem('token')
        window.location = './login.html'
      })
    })

    // 8. 一级菜单点击事件
    $('.level01').on('click', function () {
      // 9. 不直接跳转页面，而是在指定容器中跳转
      $(this)
        .addClass('active')
        .siblings()
        .removeClass('active')

      // 10. 点击到文章管理时，二级菜单出现
      if ($(this).index() == 1) {
        $('.level02').slideToggle()
        // 10.1 更改箭头方向
        $('.icon-arrowdownl').toggleClass('rotate0')
        // 二级菜单第一项高亮
        $('.level02 li')
          .eq(0)
          .addClass('active')
          .siblings('li')
          .removeClass('active')
      } else {
        $('.level02').slideUp()
        $('.icon-arrowdownl').removeClass('rotate0')
      }
    })

    // 11. 二级菜单点击事件
    $('.level02').on('click', 'li', function () {
      $(this)
        .addClass('active')
        .siblings('li')
        .removeClass('active')
    })

    // 12. 点击个人中心和头像，触发个人中心一级菜单的高亮
    $('#user2').on('click', function () {
      $('.level01')
        .eq(3)
        .click()
    })
  })
</script>
